home *** CD-ROM | disk | FTP | other *** search
/ Millennium Gold 2000 / Millennium Gold 2000 - Disc 1.iso / GNUCHESS / DSP.C < prev    next >
C/C++ Source or Header  |  1995-07-27  |  13KB  |  474 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  5.  
  6.   Modified by Daryl Baker for use in MS WINDOWS environment
  7.  
  8.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  9.   Copyright (c) 1988, 1989, 1990  John Stanback
  10.  
  11.   This file is part of CHESS.
  12.  
  13.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  14.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  15.   the consequences of using it or for whether it serves any particular
  16.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  17.   General Public License for full details.
  18.  
  19.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  20.   only under the conditions described in the CHESS General Public License.
  21.   A copy of this license is supposed to have been given to you along with
  22.   CHESS so you can know your rights and responsibilities.  It should be in a
  23.   file named COPYING.  Among other things, the copyright notice and this
  24.   notice must be preserved on all copies.
  25. */
  26.  
  27. #define NOATOM 
  28. #define NOCLIPBOARD
  29. #define NOCREATESTRUCT
  30. #define NOFONT
  31. #define NOREGION
  32. #define NOSOUND
  33. #define NOWH
  34. #define NOWINOFFSETS
  35. #define NOCOMM
  36. #define NOKANJI
  37.  
  38. #include <windows.h>
  39. #include <string.h>
  40. #include <time.h>
  41. #include <stdio.h>
  42.  
  43. #include "gnuchess.h"
  44. #include "defs.h"
  45. #include "chess.h"
  46.  
  47. #ifdef WIN32
  48. #define _BASEETC
  49. #else
  50. #define _BASEETC _based(_segname("_CODE")) 
  51. #endif
  52.  
  53. static short _BASEETC Stboard[64] =
  54. {rook, knight, bishop, queen, king, bishop, knight, rook,
  55.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  56.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  57.  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  58.  pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
  59.  rook, knight, bishop, queen, king, bishop, knight, rook};
  60.  
  61. static short _BASEETC Stcolor[64] =
  62. {white, white, white, white, white, white, white, white,
  63.  white, white, white, white, white, white, white, white,
  64.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  65.  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  66.  black, black, black, black, black, black, black, black,
  67.  black, black, black, black, black, black, black, black};
  68.  
  69. int mycntl1, mycntl2;
  70.  
  71. char mvstr[4][6];
  72. long evrate;
  73. int PositionFlag = 0;
  74.  
  75. #define pxx " PNBRQK"
  76. #define qxx " pnbrqk"
  77.  
  78. void TerminateSearch (int), Die (int);
  79.  
  80. void
  81. Initialize (void)
  82. {
  83.   mycntl1 = mycntl2 = 0;
  84. }
  85.  
  86. void
  87. ExitChess (void)
  88. {
  89.  
  90. }
  91.  
  92. void
  93. Die (int Sig)
  94. {
  95. }
  96.  
  97. void
  98. TerminateSearch (int Sig)
  99. {
  100.   flag.timeout = true;
  101.   flag.bothsides = false;
  102. }
  103.  
  104. void
  105. algbr (short int f, short int t, short int flag)
  106.  
  107. /*
  108.    Generate move strings in different formats.
  109. */
  110.  
  111. {
  112.   int m3p;
  113.  
  114.   if (f != t)
  115.     {
  116.       /* algebraic notation */
  117.       mvstr[0][0] = (char) ('a'+column (f));
  118.       mvstr[0][1] = (char) ('1'+row (f));
  119.       mvstr[0][2] = (char) ('a'+column (t));
  120.       mvstr[0][3] = (char) ('1'+row (t));
  121.       mvstr[0][4] = mvstr[3][0] = '\0';
  122.       if ((mvstr[1][0] = pxx[board[f]]) == 'P')
  123.         {
  124.           if (mvstr[0][0] == mvstr[0][2])       /* pawn did not eat */
  125.             {
  126.               mvstr[2][0] = mvstr[1][0] = mvstr[0][2];  /* to column */
  127.               mvstr[2][1] = mvstr[1][1] = mvstr[0][3];  /* to row */
  128.               m3p = 2;
  129.             }
  130.           else
  131.             /* pawn ate */
  132.             {
  133.               mvstr[2][0] = mvstr[1][0] = mvstr[0][0];  /* from column */
  134.               mvstr[2][1] = mvstr[1][1] = mvstr[0][2];  /* to column */
  135.               mvstr[2][2] = mvstr[0][3];
  136.               m3p = 3;          /* to row */
  137.             }
  138.           mvstr[2][m3p] = mvstr[1][2] = '\0';
  139.           if (flag & promote)
  140.             {
  141.               mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = qxx[flag & pmask];
  142.               mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  143.             }
  144.         }
  145.       else
  146.         /* not a pawn */
  147.         {
  148.           mvstr[2][0] = mvstr[1][0];
  149.           mvstr[2][1] = mvstr[0][1];
  150.           mvstr[2][2] = mvstr[1][1] = mvstr[0][2];      /* to column */
  151.           mvstr[2][3] = mvstr[1][2] = mvstr[0][3];      /* to row */
  152.           mvstr[2][4] = mvstr[1][3] = '\0';
  153.           strcpy (mvstr[3], mvstr[2]);
  154.           mvstr[3][1] = mvstr[0][0];
  155.           if (flag & cstlmask)
  156.             {
  157.               if (t > f)
  158.                 {
  159.                   strcpy (mvstr[1], "o-o");
  160.                   strcpy (mvstr[2], "O-O");
  161.                 }
  162.               else
  163.                 {
  164.                   strcpy (mvstr[1], "o-o-o");
  165.                   strcpy (mvstr[2], "O-O-O");
  166.                 }
  167.             }
  168.         }
  169.     }
  170.   else
  171.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  172. }
  173.  
  174. int
  175. VerifyMove (HWND hWnd, char *s, short int iop, short unsigned int *mv)
  176.  
  177. /*
  178.    Compare the string 's' to the list of legal moves available for the
  179.    opponent. If a match is found, make the move on the board.
  180. */
  181.  
  182. {
  183.   static short pnt, tempb, tempc, tempsf, tempst, cnt;
  184.   static struct leaf xnode;
  185.   struct leaf  far *node;
  186.  
  187.   *mv = 0;
  188.   if (iop == 2)
  189.     {
  190.       UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  191.       return (false);
  192.     }
  193.   cnt = 0;
  194.   MoveList (opponent, 2);
  195.   pnt = TrPnt[2];
  196.   while (pnt < TrPnt[3])
  197.     {
  198.       node = &Tree[pnt++];
  199.       algbr (node->f, node->t, (short) node->flags);
  200.       if (strcmp (s, mvstr[0]) == 0 || strcmp (s, mvstr[1]) == 0 ||
  201.           strcmp (s, mvstr[2]) == 0 || strcmp (s, mvstr[3]) == 0)
  202.         {
  203.           cnt++;
  204.           xnode = *node;
  205.         }
  206.     }
  207.   if (cnt == 1)
  208.     {
  209.       MakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst, &INCscore);
  210.       if (SqAtakd (PieceList[opponent][0], computer))
  211.         {
  212.           UnmakeMove (opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
  213.           SMessageBox (hWnd, IDS_ILLEGALMOVE, IDS_CHESS);
  214.           return (false);
  215.         }
  216.       else
  217.         {
  218.           if (iop == 1)
  219.             return (true);
  220.           UpdateDisplay (hWnd, xnode.f, xnode.t, 0, (short) xnode.flags);
  221.           if ((board[xnode.t] == pawn)
  222.               || (xnode.flags & capture)
  223.               || (xnode.flags & cstlmask))
  224.             {
  225.               Game50 = GameCnt;
  226.               ZeroRPT ();
  227.             }
  228.           GameList[GameCnt].depth = GameList[GameCnt].score = 0;
  229.           GameList[GameCnt].nodes = 0;
  230.           ElapsedTime (1);
  231.           GameList[GameCnt].time = (short) et;
  232.           TimeControl.clock[opponent] -= et;
  233.           --TimeControl.moves[opponent];
  234.           *mv = (xnode.f << 8) | xnode.t;
  235.           algbr (xnode.f, xnode.t, false);
  236.           return (true);
  237.         }
  238.     }
  239.   if (cnt > 1) SMessageBox (hWnd, IDS_AMBIGUOUSMOVE, IDS_CHESS);
  240.   return (false);
  241. }
  242.  
  243. void
  244. ElapsedTime (short int iop)
  245.  
  246. /*
  247.   Determine the time that has passed since the search was started. If
  248.   the elapsed time exceeds the target (ResponseTime+ExtraTime) then set
  249.   timeout to true which will terminate the search.
  250. */
  251.  
  252. {
  253.   et = time ((long *) 0) - time0;
  254.   if (et < 0)
  255.     et = 0;
  256.   ETnodes += 50;
  257.   if (et > et0 || iop == 1)
  258.     {
  259.       if (et > ResponseTime + ExtraTime && Sdepth > 1)
  260.         flag.timeout = true;
  261.       et0 = et;
  262.       if (iop == 1)
  263.         {
  264.           time0 = time ((long *) 0);
  265.           et0 = 0;
  266.         }
  267.       if (et > 0)
  268.         /* evrate used to be Nodes / cputime I dont` know why */
  269.         evrate = NodeCnt / (et + ft);
  270.       else
  271.         evrate = 0;
  272.       ETnodes = NodeCnt + 50;
  273.       UpdateClocks ();
  274.     }
  275. }
  276.  
  277. void
  278. SetTimeControl (void)
  279. {
  280.   if (TCflag)
  281.     {
  282.       TimeControl.moves[white] = TimeControl.moves[black] = TCmoves;
  283.       TimeControl.clock[white] = TimeControl.clock[black] = 60 * (long) TCminutes;
  284.     }
  285.   else
  286.     {
  287.       TimeControl.moves[white] = TimeControl.moves[black] = 0;
  288.       TimeControl.clock[white] = TimeControl.clock[black] = 0;
  289.       Level = 60 * (long) TCminutes;
  290.     }
  291.   et = 0;
  292.   ElapsedTime (1);
  293. }
  294.  
  295. void
  296. GetGame (HWND hWnd, char *fname)
  297. {
  298.   FILE *fd;
  299.   int c;
  300.   short sq;
  301.   unsigned short m;
  302.  
  303.   struct GameRec tmp_rec;
  304.  
  305.   if ((fd = fopen (fname, "r")) == NULL)
  306.     {
  307.       SMessageBox (hWnd, IDS_LOADFAILED, IDS_CHESS);
  308.       return;
  309.     }
  310.  
  311.   fscanf (fd, "%hd%hd%hd", &computer, &opponent, &Game50);
  312.   fscanf (fd, "%hd%hd", &castld[white], &castld[black]);
  313.   fscanf (fd, "%hd%hd", &TCflag, &OperatorTime);
  314.   fscanf (fd, "%ld%ld%hd%hd",
  315.           &TimeControl.clock[white], &TimeControl.clock[black],
  316.           &TimeControl.moves[white], &TimeControl.moves[black]);
  317.   for (sq = 0; sq < 64; sq++)
  318.     {
  319.       fscanf (fd, "%hd%hd", &m, &Mvboard[sq]);
  320.       board[sq] = (m >> 8);
  321.       color[sq] = (m & 0xFF);
  322.       if (color[sq] == 0)
  323.         color[sq] = neutral;
  324.       else
  325.         --color[sq];
  326.     }
  327.   GameCnt = 0;
  328.   c = '?';
  329.   while (c != EOF)
  330.     {
  331.       ++GameCnt;
  332.       c = fscanf (fd, "%hd%hd%hd%ld%hd%hd%hd", &tmp_rec.gmove,
  333.                   &tmp_rec.score, &tmp_rec.depth,
  334.                   &tmp_rec.nodes, &tmp_rec.time,
  335.                   &tmp_rec.piece, &tmp_rec.color);
  336.       GameList[GameCnt] = tmp_rec;
  337.       if (GameList[GameCnt].color == 0)
  338.         GameList[GameCnt].color = neutral;
  339.       else
  340.         --GameList[GameCnt].color;
  341.     }
  342.  
  343.   GameCnt--;
  344.   if (TimeControl.clock[white] > 0)
  345.     TCflag = true;
  346.   computer--;
  347.   opponent--;
  348.  
  349.   fclose (fd);
  350.  
  351.   InitializeStats ();
  352.   Sdepth = 0;
  353.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  354. }
  355.  
  356. void
  357. SaveGame (HWND hWnd, char *fname)
  358. {
  359.   FILE *fd;
  360.   short sq, i, c;
  361.  
  362.   if (NULL == (fd = fopen (fname, "w")))
  363.     {
  364.       ShowMessage (hWnd, "Not saved");
  365.       return;
  366.     }
  367.  
  368.   fprintf (fd, "%d %d %d\n", computer + 1, opponent + 1, Game50);
  369.   fprintf (fd, "%d %d\n", castld[white], castld[black]);
  370.   fprintf (fd, "%d %d\n", TCflag, OperatorTime);
  371.   fprintf (fd, "%ld %ld %d %d\n",
  372.            TimeControl.clock[white], TimeControl.clock[black],
  373.            TimeControl.moves[white], TimeControl.moves[black]);
  374.   for (sq = 0; sq < 64; sq++)
  375.     {
  376.       if (color[sq] == neutral)
  377.         c = 0;
  378.       else
  379.         c = color[sq] + 1;
  380.       fprintf (fd, "%d %d\n", 256 * board[sq] + c, Mvboard[sq]);
  381.     }
  382.   for (i = 1; i <= GameCnt; i++)
  383.     {
  384.       if (GameList[i].color == neutral)
  385.         c = 0;
  386.       else
  387.         c = GameList[i].color + 1;
  388.       fprintf (fd, "%d %d %d %ld %d %d %d\n",
  389.                GameList[i].gmove, GameList[i].score, GameList[i].depth,
  390.                GameList[i].nodes, GameList[i].time,
  391.                GameList[i].piece, c);
  392.     }
  393.   fclose (fd);
  394. }
  395.  
  396. void
  397. ListGame (HWND hWnd, char *fname)
  398. {
  399.   FILE *fd;
  400.   short i, f, t;
  401.  
  402.   if ( (fd = fopen (fname, "w")) == NULL) {
  403.       ShowMessage (hWnd, "Cannot write chess.lst");
  404.       return;
  405.   }
  406.  
  407.   fprintf (fd, "\n");
  408.   fprintf (fd, "       score  depth   nodes  time         ");
  409.   fprintf (fd, "       score  depth   nodes  time\n");
  410.   for (i = 1; i <= GameCnt; i++)
  411.     {
  412.       f = GameList[i].gmove >> 8;
  413.       t = (GameList[i].gmove & 0xFF);
  414.       algbr (f, t, false);
  415.       fprintf (fd, "%5s  %5d     %2d %7ld %5d", mvstr[0],
  416.                GameList[i].score, GameList[i].depth,
  417.                GameList[i].nodes, GameList[i].time);
  418.  
  419.       if ((i % 2) == 0)
  420.         fprintf (fd, "\n");
  421.       else
  422.         fprintf (fd, "         ");
  423.  
  424.     }
  425.   fprintf (fd, "\n\n");
  426.   fclose (fd);
  427. }
  428.  
  429. void
  430. Undo (HWND hWnd)
  431.  
  432. /*
  433.   Undo the most recent half-move.
  434. */
  435.  
  436. {
  437.   short f, t;
  438.   f = GameList[GameCnt].gmove >> 8;
  439.   t = GameList[GameCnt].gmove & 0xFF;
  440.   if (board[t] == king && distance (t, f) > 1)
  441.     (void) castle (GameList[GameCnt].color, f, t, 2);
  442.   else
  443.     {
  444.       /* Check for promotion: */
  445.       if ((color[t] == white && row (f) == 6 && row (t) == 7)
  446.           || (color[t] == black && row (f) == 1 && row (t) == 0))
  447.         {
  448.           int g, from = f;
  449.           for (g = GameCnt - 1; g > 0; g--)
  450.             if (GameList[g].gmove & 0xFF == from)
  451.               from = GameList[g].gmove >> 8;
  452.           if ((color[t] == white && row (from) == 1)
  453.               || (color[t] == black && row (from) == 6))
  454.             board[t] = pawn;
  455.         }
  456.       board[f] = board[t];
  457.       color[f] = color[t];
  458.       board[t] = GameList[GameCnt].piece;
  459.       color[t] = GameList[GameCnt].color;
  460.       if (color[t] != neutral)
  461.         Mvboard[t]--;
  462.       Mvboard[f]--;
  463.     }
  464.   if (TCflag)
  465.     ++TimeControl.moves[color[f]];
  466.   GameCnt--;
  467.   computer = otherside[computer];
  468.   opponent = otherside[opponent];
  469.   flag.mate = false;
  470.   Sdepth = 0;
  471.   UpdateDisplay (hWnd, 0, 0, 1, 0);
  472.   InitializeStats ();
  473. }
  474.